import xlrd
import xlwt
import requests
import os
import sys
def create_dir(path):
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        print('create folder :' + path)
    else:
        # print('the folder already exists' + '(%s)' % (path))
        # os.removedirs(path)
        
        # print('remove folder :' + path)
        # os.makedirs(path)
        # print('create folder :' + path)
        pass


exe_path = os.path.split(os.path.realpath(__file__))[0]
print(exe_path)

data = xlrd.open_workbook(str(sys.argv[1]))
table = data.sheets()[0]                    #通过索引顺序获取
print("表格一共有",table.nrows-1,"人")
create_dir("报备")

user_data = xlrd.open_workbook("人员列表库.xls")
user_data_table = user_data.sheets()[0]                    #通过索引顺序获取


user_data = xlrd.open_workbook("人员列表库.xls")
user_data_table = user_data.sheets()[0]                    #通过索引顺序获取

workbook = xlwt.Workbook(encoding= 'ascii')

# 创建新的sheet表
new_sheet = workbook.add_sheet("人员列表")
line_num =0
new_sheet.write(line_num,0, "RYXM")
new_sheet.write(line_num,1, "RYSFZH")
new_sheet.write(line_num,2, "RYSJH")
new_sheet.write(line_num,3, "CPH")
new_sheet.write(line_num,4, "RYSSDW")
new_sheet.write(line_num,5, "RYXWJZD")
new_sheet.write(line_num,6, "RYBZ")
line_num =1
new_sheet.write(line_num,0, "姓名")
new_sheet.write(line_num,1, "身份证号")
new_sheet.write(line_num,2, "手机号")
new_sheet.write(line_num,3, "车牌号(非大客车)")
new_sheet.write(line_num,4, "工作单位")
new_sheet.write(line_num,5, "居住地")
new_sheet.write(line_num,6, "备注")
line_num = 2



name_handler = open('报备人.txt', 'w')
for t in range(1,table.nrows):
    name = table.cell_value(t,6)
    name_handler.write(name)
    name_handler.write("\n")
    jkb_url = table.cell_value(t,7)
    xcm_url = table.cell_value(t,8)
    # print(out)
    img_data = requests.get(jkb_url).content
    


    for i in range(2,user_data_table.nrows):
        name_t = user_data_table.cell_value(i,0)
        if(name_t == name ):
            print("find")
            _id = user_data_table.cell_value(i,1)
            _phone = user_data_table.cell_value(i,2)
            _work_address = user_data_table.cell_value(i,4)
            _home_address = user_data_table.cell_value(i,5)
            _note = user_data_table.cell_value(i,6)
            print(_id)
            print(_phone)
            print(_work_address)
            print(_home_address)
            print(_note)
            # 往表格写入内容
            new_sheet.write(line_num,0, name_t)
            new_sheet.write(line_num,1, _id)
            new_sheet.write(line_num,2, _phone)

            new_sheet.write(line_num,4, _work_address)
            new_sheet.write(line_num,5, _home_address)
            new_sheet.write(line_num,6, _note)

            with open("报备/" + name +' 健康宝.jpg', 'wb') as handler:
                handler.write(img_data)
            with open("报备/" + name +' 行程码.jpg', 'wb') as handler:
                handler.write(img_data)
            line_num = line_num + 1
workbook.save("A报备人员.xls")
    









